package utilPackage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Level {

	private String Name;
	private int AlienRows;
	private int AlienColumns;
	private int NumberOfFaceHuggers;
	private int NumberOfPacks;
	private int NumberOfBombers;
	private int ExplodeCheck;
	
	public String getName() {
		return Name;
	}
	
	public void setName(String name) {
		Name = name;
	}
	
	public int getAlienRows() {
		return AlienRows;
	}
	
	public void setAlienRows(int alienRows) {
		AlienRows = alienRows;
	}
	
	public int getAlienColumns() {
		return AlienColumns;
	}
	
	public void setAlienColumns(int alienColumns) {
		AlienColumns = alienColumns;
	}
	
	public int getNumberOfFaceHuggers() {
		return NumberOfFaceHuggers;
	}
	
	public void setNumberOfFaceHuggers(int numberOfFaceHuggers) {
		NumberOfFaceHuggers = numberOfFaceHuggers;
	}
	
	public int getNumberOfPacks() {
		return NumberOfPacks;
	}
	
	public void setNumberOfPacks(int numberOfPacks) {
		NumberOfPacks = numberOfPacks;
	}
	
	public int getNumberOfBombers() {
		return NumberOfBombers;
	}
	
	public void setNumberOfBombers(int numberOfBombers) {
		NumberOfBombers = numberOfBombers;
	}
	
	public int getExplodeCheck() {
		return ExplodeCheck;
	}

	public void setExplodeCheck(int explodeCheck) {
		ExplodeCheck = explodeCheck;
	}

	@SuppressWarnings("unchecked")
	public static List MakeLevelList(String filename){
		List itemList = new ArrayList();
		Level toInsert = null;
		try {

			File file = new File(filename == "" ? "Levels.xml" : filename);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			NodeList nodeLst = doc.getElementsByTagName("Level");

			for (int s = 0; s < nodeLst.getLength(); s++) {

				Node fstNode = nodeLst.item(s);

				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

					toInsert = new Level();
					Element fstElmnt = (Element) fstNode;

					//Cannot use function because it is a string
					NodeList nameElmntLst = fstElmnt.getElementsByTagName("Name");
					Element nameElmnt = (Element) nameElmntLst.item(0);
					NodeList name = nameElmnt.getChildNodes();

					toInsert.setName(((Node) name.item(0)).getNodeValue());
					toInsert.setAlienRows(getValueOfElement("NumAliensRows", fstElmnt));
					toInsert.setAlienColumns(getValueOfElement("NumAliensColumns", fstElmnt));
					toInsert.setNumberOfFaceHuggers(getValueOfElement("NumFaceHuggers", fstElmnt));
					toInsert.setNumberOfPacks(getValueOfElement("NumPacks", fstElmnt));
					toInsert.setNumberOfBombers(getValueOfElement("NumBombers", fstElmnt));
					toInsert.setExplodeCheck(getValueOfElement("ExplodeCheck", fstElmnt));
				}
				itemList.add(toInsert);
			}
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return itemList;
	}
	
	public static int getValueOfElement(String s, Element fstElmnt){
		NodeList ElmntLst = fstElmnt.getElementsByTagName(s);
		Element Elmnt = (Element) ElmntLst.item(0);
		NodeList value = Elmnt.getChildNodes();
		if(value.item(0) != null){
			return Integer.valueOf(value.item(0).getNodeValue());
		}
		else{
			return 0;
		}
	}
}
